package pers.xx.sbm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.xx.sbm.entity.BaseMessage;
import pers.xx.sbm.entity.User;
import pers.xx.sbm.service.MessageService;
import pers.xx.sbm.utils.UserUtils;
import pers.xx.sbm.vo.ResResult;

import java.util.Date;

/**
 * @author ：xiexing
 * @description：信息Controller
 * @date ：2020/8/17 14:18
 */
@Controller
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private MessageService messageService;

    @Autowired
    private UserUtils userUtils;

    /**
     * @description 跳转到信息页面
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/")
    public String messagePage() {
        return "page/system/message";
    }


    /**
     * @description 跳转到信息发布页面
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/edit")
    public String messagePublish(Model model, Integer id) {
        if (id != null) {
            BaseMessage message = messageService.selectByPrimaryKey(id);
            model.addAttribute("message", message);
        }
        return "page/system/message_edit";
    }

    /**
     * @description 消息预览
     * @author XieXing
     * @date 2020/12/2 14:58
     */
    @RequestMapping("/preview")
    public String messagePreview(Model model, String title, String content) {
        BaseMessage message = new BaseMessage();
        message.setMsgTitle(title);
        message.setMsgContent(content);
        message.setMsgAddTime(new Date());
        model.addAttribute("message", message);
        model.addAttribute("title", "消息预览");
        return "page/system/message_detail";
    }

    /**
     * @description 跳转到信息详情页面
     * @author XieXing
     * @date 2020/8/11 15:45
     */
    @RequestMapping("/detail")
    public String messageDetail(Model model, Integer id) {
        BaseMessage message = messageService.selectByPrimaryKey(id);
        model.addAttribute("message", message);
        model.addAttribute("title", "消息详情");
        return "page/system/message_detail";
    }

    /**
     * @description 获取信息列表
     * @author XieXing
     * @date 2020/8/16 12:02
     */
    @RequestMapping("/getList")
    @ResponseBody
    public ResResult getDictList(BaseMessage message, Integer page, Integer limit, String searchContent) {
        if (!StringUtils.isEmpty(searchContent)) {
            message.setMsgTitle(searchContent);
        }
        ResResult resResult = messageService.listByPage(message, page, limit);
        resResult.setOptName("获取信息列表");
        return resResult;
    }

    /**
     * @description 保存信息
     * @author XieXing
     * @date 2020/8/17 14:22
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResResult saveDict(BaseMessage message) {
        User user = userUtils.getCurrentUser();
        message.setMsgAddby(user.getUserName());
        message.setMsgAddTime(new Date());
        message.setIsDel(0);//未删除
        message.setIsPub(0);//未发布
        message.setMsgAddbyId(user.getId());
        ResResult resResult = messageService.insert(message);
        resResult.setOptName("保存信息");
        return resResult;
    }


    /**
     * @description 更新信息的信息
     * @author XieXing
     * @date 2020/8/17 22:03
     */
    @RequestMapping("/update")
    @ResponseBody
    public ResResult updateDict(BaseMessage message) {
        User user = userUtils.getCurrentUser();
        message.setMsgAddby(user.getUserName());
        message.setMsgAddTime(new Date());
        message.setMsgAddbyId(user.getId());
        ResResult resResult = messageService.updateByPrimaryKeySelective(message);
        resResult.setOptName("更新信息的信息");
        return resResult;
    }

    /**
     * @description 删除信息 逻辑删除
     * @author XieXing
     * @date 2020/8/17 23:02
     */
    @RequestMapping("/{id}")
    @ResponseBody
    public ResResult delDict(@PathVariable(value = "id") Integer id) {
        ResResult resResult = messageService.deleteByPrimaryKey(id);
        resResult.setOptName("删除信息成功");
        return resResult;
    }


    /**
     * @description 更新信息发布状态
     * @author XieXing
     * @date 2020/8/23 21:24
     */
    @RequestMapping("/{id}/{status}")
    @ResponseBody
    public ResResult updateDictStatus(@PathVariable(value = "id") Integer id, @PathVariable(value = "status") Integer status) {
        ResResult resResult = messageService.updateDataStatus(id, status);
        resResult.setOptName("更新信息发布状态");
        return resResult;
    }

}
